from odoo import models, fields, _
from odoo.exceptions import UserError


class PurchaseRequisitionpoint(models.TransientModel):
    _name = "purchase.requisitionpoint.wizard"
    _description = "Stock Warehouse Point"

    name = fields.Char(string='Name')
    line_ids = fields.One2many(comodel_name='purchase.requisitionpoint.wizard.line',
                               inverse_name='parent_id',
                               string='Lines')

    def confirmation(self):
        self.ensure_one()
        pr_line_ids = []
        product_categ_id = False
        for record in self.line_ids:
            if record.qty_to_order <= 0:
                raise UserError(_('[%s] The quantity to order must be greater than 0') % record.product_id.name)
            product_categ_id = record.product_category_id.id
            pr_line_ids.append((0, 0, {
                'product_id': record.product_tmpl_id.id,
                'product_qty': record.qty_to_order,  # 订购数量
                'product_uom_id': record.product_uom.id,
                'requirement_date': fields.Date.today()
            }))
        if not pr_line_ids:
            return
        res = self.env['purchase.requisition'].create({
            'product_categ_id': product_categ_id,
            'pr_line_ids': pr_line_ids
        })
        self.line_ids.requisitionpoint_id.compute_qty_to_order()
        return {
            'type': 'ir.actions.act_window',
            'name': 'Purchase requisition',
            'res_model': 'purchase.requisition',
            'view_mode': 'form',
            'view_id': self.env.ref('sb_workflow_purchase.purchase_requisition_view_form').id,
            'res_id': res.id,
            'target': 'current',
        }


class PurchaseRequisitionpointLine(models.TransientModel):
    _name = "purchase.requisitionpoint.wizard.line"
    _description = "Stock Warehouse Point"

    parent_id = fields.Many2one(comodel_name='purchase.requisitionpoint.wizard', string='wizard', readonly=True)
    requisitionpoint_id = fields.Many2one(comodel_name='stock.warehouse.requisitionpoint',
                                          string='Product',
                                          readonly=True)
    product_id = fields.Many2one(comodel_name='product.product',
                                 string='Product',
                                 related='requisitionpoint_id.product_id')
    product_tmpl_id = fields.Many2one(comodel_name='product.template', related='requisitionpoint_id.product_tmpl_id')
    qty_available = fields.Float(string='Qty Available',
                                 related='requisitionpoint_id.qty_available',
                                 digits='Product Unit of Measure')
    product_min_qty = fields.Float(string='Min quantity',
                                   digits='Product Unit of Measure',
                                   related='requisitionpoint_id.product_min_qty')
    qty_to_order = fields.Float(string='To Order', digits='Product Unit of Measure')
    stock_product_uom = fields.Many2one(comodel_name='uom.uom',
                                        string='Unit of Stock',
                                        related='requisitionpoint_id.stock_product_uom')
    product_uom = fields.Many2one(comodel_name='uom.uom',
                                  string='Unit of purchase',
                                  related='requisitionpoint_id.product_uom')
    product_category_id = fields.Many2one(comodel_name='product.category',
                                          string='Unit of Measure',
                                          related='requisitionpoint_id.product_category_id')
